<?php
namespace Forum\Controller;
use Think\Controller;
class PostController extends CommonController {
    public function index(){
    }

    /*
     * 帖子列表
     */
    public function lists(){      
        $DB = M("forum_posts");

        $map['isfirst']=0;//排除主贴

        $res = $DB->where($map)->order('ctime desc')->select();

        foreach ($res as $key => $value) {


            $map_posts['pid']=$res[$key]['reply_pid'];
            $isfirst=M("forum_posts")->where($map_posts)->getField('isfirst');

        	//回复类型
        	if($isfirst) {
        		$res[$key]['reply_type']="回帖";
        	} else {
        		$res[$key]['reply_type']="回楼层";     		
        	}

            //楼层(按时间倒序计算)
            if(((count($res)-1)-$key)==0){
               $res[$key]['floor']="沙发";
            } elseif (((count($res)-1)-$key)==1) {
                $res[$key]['floor']="板凳";
            } else {
                $res[$key]['floor']="#"+($key+1);
            }

        	//用户昵称
        	$res[$key]['user'] = $this->get_user_nickname_by_userid($value['userid']);
        	
        	//回复
        	/*$map_comment['pid']=$res[$key]['pid'];
        	$res[$key]['comment_record']=M("forum_posts_comment_record")->where($map_comment)->count();*/

        	//点赞
        	/*$map_comment_like['pid']=$res[$key]['pid'];
        	$res[$key]['comment_like_record']=M("forum_posts_comment_like_record")->where($map_comment_like)->count();*/

        	//举报记录
        	$map_tipoff['pid']=$res[$key]['pid'];
        	$res[$key]['tipoff_record']=M("forum_posts_tipoff_record")->where($map_tipoff)->count();
        }

        $this->assign("results", $res);

        $this->display();
        
    }

    /*
     * 帖子回帖
     */
    public function replies() {

        if(IS_POST){          
            $data['fid']=I("post.fid");
            $data['tid']=I("post.tid");
            $data['userid']=I("post.userid");
            $data['subject']=I("post.subject");
            $data['message']=I("post.message");
            $data['reply_pid']=I("post.reply_pid");
            //图片id
            $images=I("post.images");
            $data['images']=substr($images,0,strlen($images)-1);
            $data['isfirst']=0;
            $data['invisible']=0;
            $data['ctime'] = I("server.REQUEST_TIME");

            $DB = M("forum_posts");
            $id = $DB->add($data);

            if($id){
                $this->do_post_comment(I("post.userid"),I("post.reply_pid"));//回帖
                $this->success("添加成功",U('lists'));
            }else{
                $this->error("操作失败",U('lists'));
            }
        }          
    }

    /*
     * 获取帖子的图片
     */
    public function post_images_list($pid) {

        $map_posts['pid']=$pid;

        $img_arr=M("forum_posts")->where($map_posts)->getField('images');
        $res=$this->get_post_images($img_arr);
        foreach ($res as $key => $value) {

            //$res[$key]['host']=$_SERVER['HTTP_HOST'];
            $file = file_get_contents($_SERVER['HTTP_HOST'].$res[$key]['url']);           
            $res[$key]['size']=strlen($file);
        }
        if(!empty($res)){
            $this->ajaxReturn($res,'JSON');
        }        
    }

    /*
     * 修改帖子
     * 
     * 
     */
    public function modify() {
        
        if(IS_POST){
            $pid = I("post.pid");

            //帖子
            $post = M("forum_posts");
            $post->message = I("post.message");
            $post->invisible=I("post.invisible");

            //图片id
            $images=I("post.images");
            $post->images = substr($images,0,strlen($images)-1);

            $map_posts['pid']=$pid;

            if(($post->where($map_posts)->save()) !== false){
                $this->success("修改成功");
            }else{
                $this->error("操作失败");
            }
            
        }else{
            $pid = I("get.pid");
        
            $DB = M("forum_posts");

            $res = $DB->find($pid);

            //圈子名称
            $res['forum_name'] = $this->get_forum_name_by_fid($res['fid']);

            //用户昵称
            $res['user'] = $this->get_user_nickname_by_userid($res['userid']);

            //图片
            //$res['image'] = $this->get_post_images($res['images']);
            $img_arr=$res['images'];
            if($img_arr){
                $res['image'] = $img_arr.',';
            } else {
                $res['image'] = $img_arr;
            }

            //帖子举报记录
            $map_posts_tipoff['pid']=$pid;
            $res['tipoff_record']=M("forum_posts_tipoff_record")->where($map_posts_tipoff)->count();

            $this->assign("images", $res['image']);

            $this->assign("result", $res);
            $this->display();  
        }
        
    }

    /*
     * 帖子回复记录列表
     */
    public function comment_record(){
        $pid = I("get.pid");

        //取出主贴id
        /*$map_posts['isfirst']=1;
        $map_posts['invisible']=0;
        $map_posts['tid']=$tid;
        $first_id=M("forum_posts")->where($map_posts)->getField('pid');*/

        //回复记录
        $DB = M("forum_posts");
        $map['isfirst']=0;
        $map['reply_pid']=$pid;
            
        $res = $DB->where($map)->order('ctime desc')->select();

        foreach ($res as $key => $value) {
            //用户昵称
            $res[$key]['user'] = $this->get_user_nickname_by_userid($value['userid']);

            //楼层(按时间倒序计算)
            if(((count($res)-1)-$key)==0){
               $res[$key]['floor']="沙发";
            } elseif (((count($res)-1)-$key)==1) {
                $res[$key]['floor']="板凳";
            } else {
                $res[$key]['floor']="#"+($key+1);
            }

            //回复类型
            $res[$key]['reply_type']="回楼层";

            //帖子举报记录
            $map_posts_tipoff['pid']=$res[$key]['pid'];
            $res[$key]['tipoff_record']=M("forum_posts_tipoff_record")->where($map_posts_tipoff)->count();

        }

        $this->assign("results", $res);

        //取出主题标题
        $subject = $this->get_forum_name_by_fid($tid);
        $this->assign("subject", $subject);

        $this->display();
        
    }

    /*
     * 帖子点赞记录列表
     */
    public function comment_like_record(){
        $pid = I("get.pid");

        $DB = M("forum_posts_comment_like_record");
        $map['pid']=$pid;
            
        $res = $DB->where($map)->select();

        foreach ($res as $key => $value) {
            //用户昵称
            $res[$key]['user'] = $this->get_user_nickname_by_userid($value['userid']);
        }

        $this->assign("results", $res);

        $this->display();
        
    }

    /*
     * 帖子举报记录列表
     */
    public function tipoff_record(){
        $pid = I("get.pid");

        $DB = M("forum_posts_tipoff_record");
        $map['pid']=$pid;
            
        $res = $DB->where($map)->select();

        foreach ($res as $key => $value) {
            //用户昵称
            $res[$key]['user'] = $this->get_user_nickname_by_userid($value['userid']);
        }

        $this->assign("results", $res);

        $this->display();
        
    }

    /**
     * 帖子举报处理结果
     */
    public function tipoff_update(){

        if(IS_AJAX){
            $DB = M('forum_posts_tipoff_record');
            $id = I('post.id');
            $map['id'] = $id;
            $result = I('post.result');
            if($result == 1){
                $data['result'] = 0;
            }else {
                $data['result'] = 1;
            }

            $result = $DB->where($map)->save($data);

            if($result){
                echo json_encode(1);
            }else{
                echo json_encode(0);
            }
        }
    }

    /**
     * 帖子可见状态更新
     * 1隐藏 0可见
     */
    public function posts_invisible_update(){

        if(IS_AJAX){
            $DB = M('forum_posts');
            $map['pid'] = I('post.pid');
            $invisible = I('post.invisible');
            if($invisible == 1){
                $data['invisible'] = 0;
            }else {
                $data['invisible'] = 1;
            }

            $result = $DB->where($map)->save($data);

            if($result){
                echo json_encode(1);
            }else{
                echo json_encode(0);
            }
        }
    }
}